package com.example.chaomi;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.util.Log;

/**
 * @author Giec:lijh
 *
 */
public class SystemInfo {
	
	private String attachPath = "/sys/class/unifykeys/attach";
	private String namePath = "/sys/class/unifykeys/name";
	private String readPath = "/sys/class/unifykeys/read";
	private String writePath = "/sys/class/unifykeys/write";
	
	/**
	 * read information saved in flash by name
	 * @param name name of information
	 * @return String of name
	 */
	public String readInfo(String name) {
		writeFile(attachPath, "1");
		writeFile(namePath, name);
		return readFile(readPath);
	}
	/**
	 * save information on flash
	 * @param name name of information to been saved
	 * @param content information to been saved
	 * @return true mean info has been saved
	 */
	public boolean writeInfo(String name, String content) {
		writeFile(attachPath,"1");
		writeFile(namePath, name);
		writeFile(writePath,content);
		return content.equals(readInfo(name));
	}
	private void writeFile(String path, String content)
	  {
	    File localFile = new File(path);
	    if (!(localFile.exists()))
	      return;
	    try
	    {
	      BufferedWriter localBufferedWriter = new BufferedWriter(new FileWriter(localFile), 32);
	      try
	      {
	        localBufferedWriter.write(content);
	        return;
	      }
	      finally
	      {
	        localBufferedWriter.close();
	      }
	    }
	    catch (IOException localIOException)
	    {
	    }
	}
	private String readFile(String path) {
		String content = null;
		File file = new File(path);
		if(!(file.exists()))
			return content;
		try {
			BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
			content = bufferedReader.readLine();
		}catch (IOException ioE) {
			
		}
		return content;
	};
}
